<?php

namespace App\Http\Controllers\Api;

use App\Model\UserAddressModel;
use App\Http\Controllers\Controller;
use App\Model\CityModel;
use App\User;
use Illuminate\Support\Facades\DB;


class AddressController extends Controller
{
	//地址列表
	public function list(){
		$data = [];
		$list = UserAddressModel::where('uid',request()->uid)->whereNull('cid')->orderBy('is_default','asc')->orderBy('id','desc')->get();
		foreach($list as $k=>$v){
			$data[$k]['id'] = $v['id'];
			$data[$k]['name'] = $v['name'];
			$data[$k]['phone'] = $v['phone'];
			$province = CityModel::city($v['provinceid']);
			$city = CityModel::city($v['cityid']);
			$county = CityModel::city($v['countyid']);

			$data[$k]['address'] = $province.$city.$county.$v['detail'];
			$data[$k]['is_default'] = $v['is_default'];
		}
		return ['code'=>1,'data'=>$data];
	}
	//编辑加载信息
	public function edit(){
		if(!request()->id){
			return ['code'=>0,'msg'=>retStrInfo(76)];
		}
		$list = UserAddressModel::where('id',request()->id)->where('uid',request()->uid)->first();
		if(!$list){
			return ['code'=>0,'msg'=>retStrInfo(76)];
		}

		$data['id'] = $list['id'];
		$data['name'] = $list['name'];
		$data['phone'] = $list['phone'];
		$data['provinceid'] = $list['provinceid'];
		$data['cityid'] = $list['cityid'];
		$data['countyid'] = $list['countyid'];
		$data['detail'] = $list['detail'];

		$data['province'] = UserAddressModel::city($list['provinceid']);
		$data['city'] = UserAddressModel::city($list['cityid']);
		$data['county'] = UserAddressModel::city($list['countyid']);
		$data['is_default'] = $list['is_default'];

		return ['code'=>1,'data'=>$data];
	}

	//添加地址
	public function insert(){
		if(!request()->linkman){
			return ['code'=>0,'msg'=>retStrInfo(62)];
		}
		if(!request()->linktel){
			return ['code'=>0,'msg'=>retStrInfo(63)];
		}
		$str_phone = '/^[1]\\d{10}$/';
		if (!preg_match($str_phone, request()->linktel)) {
			return ['code' => 0, 'msg' => '请输入正确的手机号'];
		}

		if(!request()->provinceid){
			return ['code'=>0,'msg'=>retStrInfo(64)];
		}
		if(!request()->cityid){
			return ['code'=>0,'msg'=>retStrInfo(65)];
		}
		if(!request()->countyid){
			return ['code'=>0,'msg'=>retStrInfo(66)];
		}
		if(!request()->detail){
			return ['code'=>0,'msg'=>retStrInfo(67)];
		}
		if(request()->is_default == 1){
			UserAddressModel::where('uid',request()->uid)->where('is_default',1)->update(['is_default'=>2]);
		}

		DB::beginTransaction();
		try {
			UserAddressModel::create([
				'uid' => request()->uid,
				'name' => request()->linkman,
				'phone' => request()->linktel,
				'provinceid' => request()->provinceid,
				'cityid' => request()->cityid,
				'countyid' => request()->countyid,
				'detail' => request()->detail,
				'is_default' => request()->is_default,
			]);
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(68)];
		}catch (Expectation $e) {
			DB::rollBack();
			return ['code' => 0, 'msg' => retStrInfo(69)];
		}
	}

	//编辑地址
	public function update(){
		if(!request()->linkman){
			return ['code'=>0,'msg'=>retStrInfo(62)];
		}
		if(!request()->linktel){
			return ['code'=>0,'msg'=>retStrInfo(63)];
		}
		$str_phone = '/^[1]\\d{10}$/';
		if (!preg_match($str_phone, request()->linktel)) {
			return ['code' => 0, 'msg' => '请输入正确的手机号'];
		}
		if(!request()->provinceid){
			return ['code'=>0,'msg'=>retStrInfo(64)];
		}
		if(!request()->cityid){
			return ['code'=>0,'msg'=>retStrInfo(65)];
		}
		if(!request()->countyid){
			return ['code'=>0,'msg'=>retStrInfo(66)];
		}
		if(!request()->detail){
			return ['code'=>0,'msg'=>retStrInfo(67)];
		}
		if(!request()->is_default){
			request()->is_default = 2;
		}
		if(request()->is_default == 1){
			UserAddressModel::where('id','<>',request()->id)->where('uid',request()->uid)->update(['is_default'=>2]);
		}
		if(!request()->id){
			return ['code'=>0,'msg'=>retStrInfo(76)];
		}
		DB::beginTransaction();
		try{
			UserAddressModel::where('id',request()->id)->update([
				'name' => request()->linkman,
				'phone' => request()->linktel,
				'provinceid' => request()->provinceid,
				'cityid' => request()->cityid,
				'countyid' => request()->countyid,
				'detail' => request()->detail,
				'is_default' => request()->is_default,
			]);
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(24)];
		}catch(Expectation $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>retStrInfo(25)];
		}
	}

	//删除地址
	public function del(){
		$list = UserAddressModel::where('id',request()->id)->first();
		if(!$list){
			return ['code'=>0,'msg'=>retStrInfo(77)];
		}

		try{
			UserAddressModel::where('id',request()->id)->delete();
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(72)];
		}catch(Expectation $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>retStrInfo(73)];
		}
	}

	//设置默认地址
	public function is_default(){
		$list = UserAddressModel::where('id',request()->id)->first();
		if(!$list){
			return ['code'=>0,'msg'=>retStrInfo(77)];
		}

		try{
			UserAddressModel::where('uid',request()->uid)->where('id','<>',request()->id)->update(['is_default'=>2]);

			UserAddressModel::where('id',request()->id)->update(['is_default'=>1]);
			DB::commit();
			return ['code'=>1,'msg'=>retStrInfo(17)];
		}catch(Expectation $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>retStrInfo(18)];
		}
	}

	//默认地址
	public function default(){
		$data['is_default'] = 2;
		$data['list'] = [];
		//用户是否有地址
		$address = UserAddressModel::where('uid',request()->uid)->first();
		if($address){
			$data['is_default'] = 1;
			//用户是否有默认地址
			$default = UserAddressModel::where('uid',request()->uid)->where('is_default',1)->first();
			$new = UserAddressModel::where('uid',request()->uid)->orderBy('id','desc')->first();
			if($default){
				$data['list']['id'] = $default['id'];
				$data['list']['name'] = $default['name'];
				$data['list']['phone'] = $default['phone'];
				$province = UserAddressModel::city($default['provinceid']);
				$city = UserAddressModel::city($default['cityid']);
				$county = UserAddressModel::city($default['countyid']);
				$data['list']['address'] = $province.$city.$county.$default['detail'];
			}else{
				$data['list']['id'] = $new['id'];
				$data['list']['name'] = $new['name'];
				$data['list']['phone'] = $new['phone'];
				$province = UserAddressModel::city($new['provinceid']);
				$city = UserAddressModel::city($new['cityid']);
				$county = UserAddressModel::city($new['countyid']);
				$data['list']['address'] = $province.$city.$county.$new['detail'];
			}
		}else{
			$data['list']['id'] = 0;
			$data['list']['name'] = '';
			$data['list']['phone'] = '';
			$data['list']['address'] = '';
		}
		return ['code'=>1,'msg'=>'返回数据','data'=>$data];
	}
}